﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;

namespace MRS.DBL.BLL
{
    /// <summary>
    /// 用户业务层
    /// </summary>
    public partial class UserMgr
    {
        static log4net.ILog _logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private readonly MRS.DBL.DAL.UserDAL _dal = new MRS.DBL.DAL.UserDAL();

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return _dal.Exists(Id);
        }

        public bool Exists(string userName) 
        {
            return _dal.Exists(userName);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddUser(MRS.DBL.Model.User model)
        {
            int newUserId = _dal.Add(model);
            _logger.InfoFormat("{0} Add User Id:{0} {1}", model.CreateUserId, newUserId, newUserId > 0 ? "Success" : "Failed");
            return newUserId;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateUser(MRS.DBL.Model.User model)
        {
            bool updateResult = _dal.Update(model);
            _logger.InfoFormat("Update User Id:{0} {1}", model.Id, updateResult ? "Success" : "Failed");
            return updateResult;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="passWord">加密后的密码</param>
        /// <returns></returns>
        public bool ChangeUserPassWord(int userId, string passWord) 
        {
            if (string.IsNullOrWhiteSpace(passWord)) 
            {
                _logger.InfoFormat("User Id:{0} ChangeUserPassword Failed: The password is Null", userId);
                return false;
            }
            bool result = _dal.ChangeUserPassWord(userId, passWord);
            _logger.InfoFormat("User Id:{0} ChangeUserPassword {1}", userId, result ? "Success" : "Failed");
            return result;
        }

        /// <summary>
        /// 修改用户在线状态
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="isOnline">True 在线  False 离线</param>
        public void ChangeUserOnLineStatus(int userId, bool isOnline) 
        {
            _dal.ChangeUserOnLineStatus(userId, isOnline);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int userId)
        {
            bool delResult = _dal.Delete(userId);
            _logger.InfoFormat("Delete User Id:{0} {1}", userId, delResult ? "Success" : "Failed");
            return delResult;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool BatchDeleteUsers(string Idlist) 
        {
            return _dal.BatchDeleteUsers(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MRS.DBL.Model.User GetModel(int Id)
        {

            return _dal.GetModel(Id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public MRS.DBL.Model.User[] GetList()
        {
            return _dal.GetList();
        }

        /// <summary>
        /// 获得数据列表(分页)
        /// </summary>
        public MRS.DBL.Model.User[] GetList(MRS.DBL.Model.User model, MRS.DBL.Model.User model2, int pageindex, int pagesize, out int pagecount)
        {
            IDbConnection conn = Common.SQL.DBPool.Instance().GetConnection();

            MRS.DBL.BLL.UserPageBLL oPage = new MRS.DBL.BLL.UserPageBLL();
            oPage.CurPage = pageindex;
            oPage.PageSize = pagesize;
            oPage.GetUserList(model, model2, true);
            Model.User[] objs = oPage.GetPage();
            pagecount = oPage.TotalPage;
            if (objs == null)
            {
                pagecount = 0;
                return null;
            }

            return objs;
        }

        #endregion

    }
}